﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Business;
using System.Web.UI.HtmlControls;
using ECommerce.Library.ECommerce;
using eShop.Utility;
using eShop.Model;
using System.Linq;

public partial class controls_wucProductDetails : System.Web.UI.UserControl
{
    public String ProductPath
    {
        get
        {
            try
            {
                return Page.RouteData.Values["path"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    public int ProductId
    {
        get
        {
            return Convert.ToInt32(this.ViewState["_PRODUCTID"]);
        }
        set
        {
            this.ViewState["_PRODUCTID"] = value;
        }
    }

    public void BindDetails()
    {
        if (!Page.IsPostBack)
        {
            BindProductInformation();
            BindProductAdditionalImages();
            LoadReviews();
            if (SessionManager.IsUserLoggedIn)
            {
                pnlMisc.Visible = true;
                pnlReviews.Visible = true;
            }
            else
            {
                pnlMisc.Visible = false;
                pnlReviews.Visible = false;
            }
        }
    }

    private void BindProductInformation()
    {
        Product product = null;
        product = ProductManager.Load(ProductPath);
        
        
        ProductId = product.ProductId;
        lblProductName.Text = product.ProductName;
        lblItemNo.Text = product.ItemNumber;
        if (product.IsClearenceItem)
        {
            lblPrice.Text = "<span class='product-detail-price-x'>" + Utility.FormatPriceText(product.ProductPrice) + "</span>" + Utility.FormatPriceText(product.ClearencePrice);
        }
        else
        {
            lblPrice.Text = Utility.FormatPriceText(product.ProductPrice);
        }

        lProductDescription.Text = Server.HtmlDecode(product.ProductDescription);
        lblSpecifications.Text =Server.HtmlDecode(product.Specification);

        ddlStocks.DataSource = product.Stocks;
        ddlStocks.DataValueField = "StockId";
        ddlStocks.DataTextField = "FormattedDescription";
        ddlStocks.DataBind();

        rptSiteMap.DataSource = CategoryManager.GetSubCategorySiteMapTree(SiteUtility.SuperRootCategoryId, product.Category.Id);
        rptSiteMap.DataBind();

        hlkProductLink.Text = product.ProductName;
    }

    private void BindProductAdditionalImages()
    {
        IList<ProductImage> additionalImages = ProductManager.GetProductAdditionalImagesByProductId(ProductId);
        rptAdditionalViews.DataSource = additionalImages;
        rptAdditionalViews.DataBind();
    }

    protected void rptSiteMap_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hlkSiteMap = (HyperLink)e.Item.FindControl("hlkSiteMap");
                KeyValuePair<string, string> node = (KeyValuePair<string, string>)e.Item.DataItem;
                hlkSiteMap.NavigateUrl = node.Value;
                hlkSiteMap.Text = node.Key.ToLower() == "none" ? "Home" : node.Key;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void LoadReviews()
    {
        List<ProductReview> reviews = ProductManager.GetProductReviewByProduct(ProductId);
        if (reviews != null && reviews.Count > 0)
        {
            decimal averageLength = (decimal)reviews.Average(r => r.RatingValue.HasValue ? r.RatingValue.Value : 0);
            litReviewAvg.Text = averageLength.ToString();
            ReviewCount.Text = reviews.Count.ToString();
        }

    }
}